package cn.wolfcode.trip.base.service.impl;


import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.TravelContent;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.TravelContentMapper;
import cn.wolfcode.trip.base.mapper.TravelMapper;
import cn.wolfcode.trip.base.mapper.UserMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.service.IUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class TravelServiceImpl implements ITravelService {

    @Autowired
    private TravelMapper travelMapper;
    @Autowired
    private TravelContentMapper travelContentMapper;


    @Override
    public PageInfo<Travel> selectForList(TravelQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(),qo.getOrderBy());
        List<Travel> list = travelMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    @Override
    public void saveOrUpdate(Travel travel) {

        travel.setLastUpdateTime(new Date());
        TravelContent travelContent = travel.getTravelContent();

        if (travel.getId() == null) {
            //填充默认数据
            travel.setCreateTime(new Date());
            travelMapper.insert(travel);

            //同步保存文章的内容,同时设置关联的主键
            travelContent.setId(travel.getId());
            travelContentMapper.insert(travelContent);

        }else {

            travelMapper.updateByPrimaryKey(travel);

            //同步保存文章的内容,同时设置关联的主键
            travelContent.setId(travel.getId());
            travelContentMapper.updateByPrimaryKey(travelContent);
        }
    }

    @Override
    public Travel get(Long id) {
        return travelMapper.selectByPrimaryKey(id);
    }

    @Override
    public TravelContent getContent(Long id) {
        return travelContentMapper.selectByPrimaryKey(id);
    }

    @Override
    public void changeState(Travel travel) {
        if (travel.getState()==2){
            travel.setReleaseTime(new Date());
        }
        travelMapper.changeState(travel);
    }
}
